/*
 *  main.cpp
 *  Copyright 2008 KoN Team. All rights reserved.
 *
 */

#include <irrlicht.h>

#include "event.h"

using namespace irr;

int main(int agrc, char **argv) {
    // create event receiver
    GameEventReceiver ger;
    
    IrrlichtDevice *device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(640, 480), 16, false, false, false, &ger);
    if (!device) {
        return 1;
    }
    
    video::IVideoDriver *driver = device->getVideoDriver();
    scene::ISceneManager *smgr = device->getSceneManager();
    gui::IGUIEnvironment *guienv = device->getGUIEnvironment();
    
    scene::ISceneNode *node = smgr->addCubeSceneNode(1.0);
    if (node) {
        node->setMaterialTexture(0, driver->getTexture("../../resources/wall.jpg"));
        node->setMaterialFlag(video::EMF_LIGHTING, false);
        scene::ISceneNodeAnimator* anim = smgr->createFlyCircleAnimator(core::vector3df(0,0,0), 5.0f);
        if (anim) {
            node->addAnimator(anim);
            anim->drop();
        }
    }
    
    smgr->loadScene("../../resources/TestScene/Scene.irr");
    
    scene::ISceneNode *rootnode = smgr->getRootSceneNode();
    scene::ISceneNode *grassnode = smgr->getSceneNodeFromName("Plane");
    
    scene::ITriangleSelector *sel = smgr->createTriangleSelectorFromBoundingBox(grassnode);
    grassnode->setTriangleSelector(sel);
    sel->drop();
    
    //core::list<scene::ISceneNode*> children = rootnode->getChildren();
    grassnode->setDebugDataVisible(scene::EDS_BBOX);
    
    //for (core::list<scene::ISceneNode*>::Iterator it = children.begin(); it != children.end(); it++) {
    //    (*it)->setDebugDataVisible(scene::EDS_BBOX);
    //}
    
    smgr->addCameraSceneNode(0, core::vector3df(5,5,-5), core::vector3df(0,0,0));
    scene::ICameraSceneNode *camera = smgr->getActiveCamera();
    scene::ISceneCollisionManager *cmgr = smgr->getSceneCollisionManager();
    
    int lastFPS = -1;
    
    while (device->run() && driver) {
        
        // keyboard map scrolling
        
        if (ger.IsKeyDown(irr::KEY_KEY_W)) {
            core::vector3df v = camera->getPosition();
            core::vector3df w = camera->getTarget();
            v.Y += 0.02f;
            w.Y += 0.02f;
            camera->setPosition(v);
            camera->setTarget(w);
        }
        else if (ger.IsKeyDown(irr::KEY_KEY_S)) {
            core::vector3df v = camera->getPosition();
            core::vector3df w = camera->getTarget();
            v.Y -= 0.02f;
            w.Y -= 0.02f;
            camera->setPosition(v);
            camera->setTarget(w);
        }
        else if (ger.IsKeyDown(irr::KEY_KEY_A)) {
            core::vector3df v = camera->getPosition();
            core::vector3df w = camera->getTarget();
            v.X -= 0.02f;
            w.X -= 0.02f;
            camera->setPosition(v);
            camera->setTarget(w);
        }
        else if (ger.IsKeyDown(irr::KEY_KEY_D)) {
            core::vector3df v = camera->getPosition();
            core::vector3df w = camera->getTarget();
            v.X += 0.02f;
            w.X += 0.02f;
            camera->setPosition(v);
            camera->setTarget(w);
        }
        else if (ger.IsKeyDown(irr::KEY_UP)) {
            core::vector3df v = camera->getPosition();
            core::vector3df w = camera->getTarget();
            v.Z += 0.02f;
            w.Z += 0.02f;
            camera->setPosition(v);
            camera->setTarget(w);
        }
        else if (ger.IsKeyDown(irr::KEY_DOWN)) {
            core::vector3df v = camera->getPosition();
            core::vector3df w = camera->getTarget();
            v.Z -= 0.02f;
            w.Z -= 0.02f;
            camera->setPosition(v);
            camera->setTarget(w);            
        }
        
        core::position2d<s32> m = device->getCursorControl()->getPosition();
        
        driver->beginScene(true, true, video::SColor(255, 100, 100, 140));
        
        core::line3df ray = cmgr->getRayFromScreenCoordinates(m);
        
        smgr->drawAll();
        
        core::vector3df collpoint;
        core::triangle3df colltriangle;
        bool b = cmgr->getCollisionPoint(ray, grassnode->getTriangleSelector(), collpoint, colltriangle);
        if (b && ger.IsMouseDown(0)) {
            //printf("coll point: %f %f %f\n", collpoint.X, collpoint.Y, collpoint.Z);
            scene::ISceneNodeAnimator* anim = smgr->createFlyStraightAnimator(node->getPosition(), collpoint, 500);
            if (anim) {
                node->addAnimator(anim);
                anim->drop();
            }
        }
        
        driver->endScene();
        
        int fps = driver->getFPS();
        core::stringw tmp(L"Irrlicht [");
        tmp += driver->getName();
        tmp += L"] running at fps: ";
        tmp += fps;
        tmp += core::stringw(L" (") + m.X + core::stringw(L",") + m.Y + core::stringw(L")");
        device->setWindowCaption(tmp.c_str());
        lastFPS = fps;        
    }
    
    device->drop();
    return 0;    
}
